// CyTargetLinker RIN conversion,
//
// scripts to create RIN network files for several online databases
// can be used with the CyTargetLinker app for Cytoscape
//
// Copyright 2011-2013 Department of Bioinformatics - BiGCaT, Maastricht University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package cytargetlinker.conversion.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.bridgedb.DataSource;
import org.bridgedb.IDMapper;
import org.bridgedb.IDMapperException;
import org.bridgedb.Xref;

import cytargetlinker.conversion.graph.Graph;
import cytargetlinker.conversion.graph.Graph.Node;

/**
 * Class that represents a microRNA node in a RIN 
 * the main identifier system is miRBase mature sequence accession number, 
 * but current and previous ids/names are added as attributes
 * 
 * @author martina
 *
 */
public class MiRNANode implements INode {

	private String id;
	private List<String> names;
	private String label;
	private String biologicalType = "microRNA";
	
	private MiRNANode() {
		names = new ArrayList<String>();
	}
	
	public static MiRNANode createMiRNANode(String name, IDMapper mapper, DataSource in) {
		Xref xrefIn = new Xref(name, in);
		try {
			Set<Xref> result = mapper.mapID(xrefIn, DataSource.getBySystemCode("Mbm"));
			if(result.size() == 1) {
				MiRNANode node = new MiRNANode();
				node.setId(result.iterator().next().getId());
				Set<Xref> names = mapper.mapID(xrefIn, DataSource.getBySystemCode("Mb"));
				for(Xref x : names) {
					if(!node.getNames().contains(x.getId())) node.getNames().add(x.getId());
				}
				node.setLabel(name);
				return node;
			}
		} catch(IDMapperException e) {
			return null;
		}
		return null;
	}
	
	public Node getNode(Graph graph) {
		Node node = graph.addNode(getId());
		node.appendAttribute("mimat id", getId());
		node.appendAttribute("identifiers", getIdentifiers());
		node.appendAttribute("label", getLabel());
		node.appendAttribute("name", getLabel());
		node.appendAttribute("biologicalType", getBiologicalType());
		return node;
	}
	
	private String getIdentifiers() {
		String ids = "[";
		ids = ids + getId();
		
		for(String name : getNames()) {
			ids = ids + "," + name;
		}
		
		ids = ids + "]";
		return ids;
	}

	// SETTERS & GETTERS
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<String> getNames() {
		return names;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getBiologicalType() {
		return biologicalType;
	}
}
